import 'package:boss/color_constant.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';


class SearchBar extends StatefulWidget {
  final String hintText;
  final FocusNode focusNode;
  final TextEditingController controller;
  final ValueChanged<String> onChanged;
  final VoidCallback onEditingComplete;
  final bool enabled;

  const SearchBar({
    Key key,
    this.hintText,
    this.focusNode,
    this.controller,
    this.onChanged,
    this.onEditingComplete,
    this.enabled = true,
  }) : super(key: key);

  @override
  _SearchBarState createState() => _SearchBarState();
}

class _SearchBarState extends State<SearchBar> {
  FocusNode focusNode;

  @override
  void initState() {
    super.initState();

    focusNode = widget.focusNode ?? FocusNode();
  }

  @override
  Widget build(BuildContext context) {
    final border = NoInputBorder();
    final height = 60.w;

    Widget child = Container(
      child: Stack(
        alignment: Alignment.centerLeft,
        children: <Widget>[
          Container(
            height: height,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              border: Border.all(
                color: ColorConstant.e5e5,
                width: 1.px,
              ),
              borderRadius: BorderRadius.all(
                Radius.circular(height),
              ),
            ),
            child: TextFormField(
              keyboardAppearance: Brightness.light,
              focusNode: focusNode,
              controller: widget.controller,
              onChanged: widget.onChanged,
              onEditingComplete: widget.onEditingComplete,
              decoration: InputDecoration(
                hintText: widget.hintText,
                hintStyle: TextStyleConstant().hintTextStyle,
                contentPadding: EdgeInsets.symmetric(
                  vertical: 0.w,
                  horizontal: 70.w,
                ),
                border: border,
                enabledBorder: border,
                focusedBorder: border,
                focusedErrorBorder: border,
                errorBorder: border,
                disabledBorder: border,
                isDense: true,
              ),
              style: TextStyleConstant().normal_1_26,
              enabled: widget.enabled,
            ),
          ),
          Positioned(
            left: 18.w,
            child: Image.asset(
              'assets/image/search_icon.png',
              width: 30.w,
              height: 30.w,
              fit: BoxFit.cover,
            ),
          )
        ],
      ),
    );

    if (widget.enabled ?? false) {
      child = GestureDetector(
        onTap: () {
          focusNode.requestFocus();
        },
        child: child,
      );
    }

    return child;
  }
}
